package com.eclink.asset.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.common.domain.dto.CommonUserDTO;
import com.eclink.asset.domain.CommonUser;
import com.eclink.asset.mapper.CommonUserMapper;
import com.eclink.asset.setup.service.CompanyService;
import com.eclink.asset.setup.service.DeptService;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
public class CommonUserService {

    @Autowired
    private CommonUserMapper commonUserMapper;

    @Resource
    private CompanyService companyService;

    @Resource
    private SysDeptServiceImpl sysDeptService;
    @Autowired
    private DeptService deptService;

    public String getNickNameById(Serializable userId) {
        QueryWrapper<CommonUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<CommonUser> list = commonUserMapper.selectList(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.get(0).getNickName();
        }
        return null;
    }

    public CommonUserDTO getUserSimpleById(Serializable userId) {
        QueryWrapper<CommonUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        CommonUser user = commonUserMapper.selectOne(queryWrapper);
        if (user == null) {
            return null;
        }
        CommonUserDTO userDTO = new CommonUserDTO();
        BeanUtil.copyProperties(user, userDTO);

        return userDTO;
    }

    public CommonUserDTO getUserById(Serializable userId) {

        CommonUserDTO userDTO = getUserSimpleById(userId);
        if (userDTO.getCompanyId() != null && userDTO.getCompanyId() != 0) {
            userDTO.setCompanyName(companyService.getNameById(userDTO.getCompanyId()));
        }
        if (userDTO.getDeptId() != null && userDTO.getDeptId() != 0) {
            userDTO.setDeptName(sysDeptService.getDeptNameById(userDTO.getDeptId()));
        }

        return userDTO;
    }

    public List<CommonUser> selectAll() {
        QueryWrapper<CommonUser> qw = new QueryWrapper<>();
        return commonUserMapper.selectList(qw);
    }

    public Page<CommonUser> selectList(Pageable page) {
        QueryWrapper<CommonUser> qw = new QueryWrapper<>();
        return commonUserMapper.selectPage(new Page<>(page.getPageNumber(), page.getPageSize()),qw);
    }

    public List<CommonUser> selectDetailAll() {
        List<CommonUser> list = selectAll();
        for (CommonUser commonUser : list) {
            if (commonUser.getDeptId()!= null && commonUser.getDeptId() != 0) {
                commonUser.setDeptName(deptService.getNameById(commonUser.getDeptId()));
            }
            if (commonUser.getCompanyId()!= null && commonUser.getCompanyId() != 0) {
                commonUser.setCompanyName(companyService.getNameById(commonUser.getCompanyId()));
            }
        }
        return list;
    }

}
